<?php
$device_obj = new device();
$project_obj = new project();
$admin_obj = new admin();
$zip_obj = new zip();

$action_ary = array(
	'checkin' => 'device_act_record',
	'record'=>'device_act_record',
	'edit'=>'device_act_edit', //修改登记记录
	'edits'=>'device_act_edits',
	'add' => 'device_act_add',//添加设备
	'unique' => 'device_act_unique',//检查设备号唯一性
	'ajax_list' => 'device_act_ajax_list', //ajax获取设备列表
	'improt_out' => 'device_act_improt_out', //导出数据csv
	'improt_out_all' => 'device_act_improt_out_all', //导出数据csv
	'improt_car_out'=>'device_act_improt_car_out',
	'time_out'=>'device_act_time_out',
	'device_act_time_out'=>'device_act_time_out',
	'improt_time_all' => 'device_act_improt_time_all', //导出数据csv
	'zip_pack' => 'device_act_zip_pack', //项目图片zip打包
	'checkin_del' => 'device_act_checkin_del', //删除登记记录
	'fileout'=>'device_act_fileout',  //导出文件
	'car_setedit'=>'device_act_car_setedit',
	'cardel'=>'device_act_cardel',
	'getparam'=>'device_act_getparam', //登录系统后获取挖机编号和线路编号接口  接口
	'carrecord'=>'device_act_carrecord'	//渣车登记接口
);


function device_act_cardel(){
	$id=request("id",false,true,false);
	$rtn['state']=false;
	if(empty($id)){
		$rtn['msg']='参数信息不正确';
		exit(json_encode($rtn));
	}

	global $device_obj,$admin_obj;
	$user_info = $admin_obj->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	$uid=$user_info['id'];
	$res=$device_obj->setinfo_del($id,$uid);
	if($res){
		$rtn['state']=true;
		$rtn['msg']='删除成功';
		exit(json_encode($rtn));
	}

	$rtn['msg']='删除失败';
	exit(json_encode($rtn));
}


function device_act_car_setedit(){
	$id=request('id',false,true,false);
	$type=request('type',false,true,false,1);
	$uid=request('pid',false,true,false,1);
	$code=request('code');
	$rtn['state']=false;
	if(empty($code) || strlen($code)>20){
		$rtn['msg']='请正确输入编号信息';
		exit(json_encode($rtn));
	}
	$comment=request('comment');
	if(strlen($comment)>200){
		$rtn['msg']='备注信息太长';
		exit(json_encode($rtn));
	}

	$state=request('state',false,true,false,1);
	$sort=request('sort',false,true,false,0);
	$phone=request('phone',false,false,false,'');
	$cartype=request('cartype',false,false,false,'');
	$yun_min=request('yun_min',false,true,false,0);
	$yun_max=request('yun_max',false,true,false,0);
	$data=compact('type','uid','code','comment','state','sort','phone','cartype','yun_min','yun_max');
	//查询项目信息
	global $device_obj;
	//修改信息
	if($id){
		$res = $device_obj->setinfo_edit($id, $data);
		if($res){
		   $rtn['state']=true;
		   $rtn['msg']='修改成功';
		   exit(json_encode($rtn));
		}
	}

	$res = $device_obj->setinfo_add($data);
	if($res){
		$rtn['state']=true;
		$rtn['msg']='添加成功';
		exit(json_encode($rtn));
	}
}



function device_act_fileout(){
	global $device_obj,$_site;

	$data['pid']=request('pid',false,true,false,0);
	$data['state']=request('state',false,true,false,1);
	$data['address']=request('address',true,false,false,0);//作业点id
	$data['no']=request('no',true,false,false,0);
	$data['type']=$type=request('type',true,false,false,1);
	$data['truck']=$truck=request('truck',true,false,false,0);
	$data['dust']=$dust=request('dust',true,false,false,0);
	$data['onload']=$onload=request('onload',true,false,false,0);
	$data['dname']=$dname=request('dname');
	$data['start_time']=request('start_time');
	$data['end_time']=request('end_time');
	$data['imgout']=request('imgout',false,true,false,0);
	$data['img_del']=0;

	$pagesize=1000; //每次追多导出x张图片
	$res=$device_obj->get_checkin_list($data,1,$pagesize,"img");

	if(empty($res['rows'])){
		$rtn['state']=false;
		$rtn['info']='没有图片可以导出';
		exit(json_encode($rtn));
	}

	$_root=$_site->_root();
	foreach($res['rows'] as $val){
		$zip_path[]='.'.$val['img'];
	}

	$_zip='./data/zip/';


	$s = Tozip($zip_path,$_zip,1);
	$rtn['state']=true;
	$rtn['info']=$zip_path[0];
	exit(json_encode($rtn));
}




function device_act_edit(){
	global $device_obj;

	$data['id']=request('id',false,true);
	$info=$device_obj->get_record_info($data['id']);
	if(empty($info))return false;

	$data['truck']=request('truck');
	$data['dust']=request('dust');
	$data['onload']=request('onload');

	//重新编号
	$dust_ary=array(1=>'Ⅰ',2=>'Ⅱ',3=>'Ⅲ');
	$onload_ary=array(1=>'K',2=>'Z');
	$data['code']=$info['dname'].'_'.date('YmdHis',$info['ctime']).'_ZC_'.strtoupper($data['truck']).'_'.$dust_ary[$data['dust']].'_'.$onload_ary[$data['onload']];

	$res=$device_obj->checkin_up_data($data);
	$rtn = array('state'=>false);
	if($res){
		$truckType=array('a'=>'A 型','b'=>'B 型','c'=>'C 型');
		$dustType=array('1'=>'Ⅰ类','2'=>'Ⅱ类','3'=>'Ⅲ 类');
		$onloadType=array(1=>"空车",2=>"重车");
		$data['truck']=$truckType[$data['truck']];
		$data['dust']=$dustType[$data['dust']];
		$data['onload']=$onloadType[$data['onload']];

		$rtn['state'] = true;
		$rtn['info'] = $data;
		echo_json($rtn);
		exit;
	}

	$rtn['msg'] = 1003;
	echo_json($rtn);
}


function device_act_edits(){
	global $device_obj,$admin_obj;

	$data['id']=request('id',false,true);
	$info=$device_obj->get_record_info($data['id']);
	if(empty($info))return false;

	$user_info = $admin_obj->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	if($user_info['group']!=6 && $user_info['id']!=$info['xid']){
		$rtn['msg']='非法操作';
		exit(json_encode($rtn));
	}

	$data['posi']=request('posi');
	$data['wj_no']=request('wj_no');
	$data['wj_xh']=request('wj_xh');
	$data['line']=request('line');
	//$data['disall']=request('disall');
	$data['yunno']=request('yunno');


	//重新编号

	$res=$device_obj->checkin_up_data($data);
	$rtn = array('state'=>false);
	if($res){
		$rtn['state'] = true;
		$rtn['info'] = $data;
		echo_json($rtn);
		exit;
	}

	$rtn['msg'] = 1003;
	echo_json($rtn);
}



function device_act_checkin_del(){
	global $admin_obj,$_site,$device_obj;
	if(!$admin_obj->out_cookie()){
		echo_json(array('state'=>false,'msg'=>1411));
	}
	$times = request('times');
	if(!$times){
		echo_json(array('state'=>false,'msg'=>1452));
	}
	$pid = request('pid',false,true);
	if(!$pid){
		echo_json(array('state'=>false,'msg'=>1452));
	}

	$times = strtotime($times);

	$s = $device_obj->del_checkin(array('pid'=>$pid,'times'=>$times+86399));
	if(!$s){
		echo_json(array('state'=>false,'msg'=>1453,'row'=>$s));
	}
	$dir_time = date('Ymd',$times);
	///upload/item/1/20140623/14035040399990.jpg

	while (is_dir($_site->_root().'/'.$_site->site_config['site']['upload_dir'].'/item/'.$pid.'/'.$dir_time)){
		$dir=$_site->_root().'/'.$_site->site_config['site']['upload_dir'].'/item/'.$pid.'/'.$dir_time;
		deldir($dir);
		$dir_time=date('Ymd',strtotime('-1 day',strtotime($dir_time)));


	}
	echo_json(array('state'=>true,'msg'=>1454,'row'=>$s));
}
//先删除目录下的文件：
function deldir($dir){

	$dh=opendir($dir);
	while ($file=readdir($dh)) {
		if($file!="." && $file!="..") {
			$fullpath=$dir."/".$file;
			if(!is_dir($fullpath)) {
				unlink($fullpath);
			} else {
				deldir($fullpath);
			}
		}
	}
	closedir($dh);
	return rmdir($dir);
}
/**
* @author xiezhihui 2014-6-21 12:51:21
* 打包并下载项目下的图片zip
*
*/
function device_act_zip_pack(){
	global $zip_obj,$admin_obj,$_site,$device_obj;
	if(!$admin_obj->out_cookie()){
		echo_json(array('state'=>false,'msg'=>1411));
	}
	$item_id = request('item_id',false,true);
	if(!$item_id){
		echo_json(array('state'=>false,'msg'=>1451));
	}
	$item_name = request('item_name');
	if(!$item_name){
		echo_json(array('state'=>false,'msg'=>1451));
	}

	//时间取值
	$data['start_time']=request('start_time')?request('start_time'):0;
	$data['end_time']=request('end_time')?request('end_time')+3481:0;

	if($data['start_time'] && $data['end_time']){
		$dir_time = ceil(ABS($data['end_time']-$data['start_time'])/86400);
	}elseif($data['start_time'] && !$data['end_time']){
		$max = $device_obj->get_chekin_info_pid($item_id,'desc');
		$data['end_time'] = $max[0]['time'];
		$dir_time = ceil(ABS($data['end_time']-$data['start_time'])/86400);
	}elseif(!$data['start_time'] && $data['end_time']){
		$min = $device_obj->get_chekin_info_pid($item_id,'asc');
		$data['start_time'] = $min[0]['time'];
		$dir_time = ceil(ABS($data['end_time']-$data['start_time'])/86400);
	}

	$item_path =$_site->_root().'/'.$_site->site_config['site']['upload_dir'].'/item/'.$item_id;
	if(isset($dir_time)){
		$zip_path = array();
		$endtime = date('Ymd',$data['end_time']);
		$i = 0;
		while ($starttime!=$endtime){
			$starttime = date('Ymd',strtotime("+$i day",$data['start_time']));
			$tmp_dir=$item_path.'/'.date('Ymd',strtotime("+$i day",$data['start_time']));
			if(is_dir($tmp_dir)){
				$zip_path[]=$item_path.'/'.date('Ymd',strtotime("+$i day",$data['start_time']));
			}
			$i++;
		}

	}else{
		$zip_path =$_site->_root().'/'.$_site->site_config['site']['upload_dir'].'/item/'.$item_id;
	}
	if(!is_dir($zip_path) && !is_array($zip_path)){
		echo_json(array('state'=>false,'msg'=>1449));
	}
	$s = Tozip($zip_path,'./'.$item_name,1);
	echo_json(array('state'=>false,'msg'=>1450));
}


/**
 * 导出装车统计报表信息
 * @author leiqianyong 2015-10-19 10:21 AM
 */
function device_act_improt_out(){
	global $device_obj,$project_obj;
	$data = $_GET;

	//$list=$device_obj->get_report($data);
	ini_set('memory_limit','1024M');


	//新计算方式
	$list=$device_obj->get_report_all($data,1);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}

	//获取挖机计时数据
	$list_time=$device_obj->get_act_time_report_parse($data);
	$ctime_ary=false;
	if($list_time){
		foreach($list_time as $key=>$cv){
			$time_sum=$cv['sum_time'];
			$ctime_ary[$key]=$time_sum;
			$ctime_rtn[$key]=$cv;
		}
	}

	//项目工时间隔
	$pinfo=$project_obj->info($data['pid'],'time_zc,config');
	$time_zc=($pinfo && $pinfo['time_zc']>0)?$pinfo['time_zc']:300;

		foreach($list as $key=>$cv){
			//$dname=$cv['dname']; //挖机编号
			$dname=md5($cv['dname'].'_'.$cv['jsno'].'_'.$cv['dkno']);

			//计时拍照模式
			if($cv['gcat']){
				$grtn[$dname]['gcat']=$grtn[$dname]['gcat']?($grtn[$dname]['gcat']+1):1;
				$grtn[$dname]['ginfo']=$cv;
				continue;
			}


			$rtn[$dname]['dname']=$cv['dname'];
			$rtn[$dname]['car_type']=$cv['car_type'];
			$rtn[$dname]['downer']=$cv['downer'];
			$rtn[$dname]['jsno']=$cv['jsno'];
			$rtn[$dname]['doper']=$cv['doper'];
			$rtn[$dname]['dkno']=$cv['dkno'];
			$key=$cv['truck'].'_'.$cv['dust'];
			$rtn[$dname][$key]=$rtn[$dname][$key]?($rtn[$dname][$key]+1):1;


			//累计时间
			//前面的记录
			$lastime=$rtn[$dname]['lastime']?$rtn[$dname]['lastime']:0;
			$grtime=$cv['ctime']-$lastime;
			//异常时间处理：小于等于0，大于等于13分钟，设置成标准时间
			if($grtime<=0 || $grtime>780)$grtime=$time_zc;
			$rtn[$dname]['altime']=$rtn[$dname]['altime']?($rtn[$dname]['altime']+$grtime):$grtime;
			$rtn[$dname]['lastime']=$cv['ctime']; //作为下次时间计算的上一条
		}


		//计时拍照模式的导入
	    if($grtn){
	    	foreach($grtn as $key=>$vo){
	    		if($rtn[$key]){
                    $rtn[$key]['gcnum']=$vo['gcat'];
			    }else{
                    $rtn[$key]=$vo['ginfo'];
				    $rtn[$key]['gcnum']=$vo['gcat'];
			    }
		    }
	    }




	$title_ext = '挖机装车统计信息表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('序号','挖机编号','设备型号','产权人','机手编号','机手姓名','地块编号','Ⅰ类A型','Ⅰ类B型','Ⅰ类C型','Ⅱ类A型','Ⅱ类B型','Ⅱ类C型','Ⅲ类A型','Ⅲ类B型','Ⅲ类C型','装车车数','装车工时','辅助计时','计时车数','合计车数','总工时','分钟/车','车/小时','备注','签字');
	if($pinfo['config']){
		$config=stringToarray($pinfo['config']);
		$a=$config['a']?$config['a']:'A';
		$b=$config['b']?$config['b']:'B';
		$c=$config['c']?$config['c']:'C';
		$x=$config['x']?$config['x']:'Ⅰ';
		$y=$config['y']?$config['y']:'Ⅱ';
		$z=$config['z']?$config['z']:'Ⅲ';
		$title[7]=$x.$a;
		$title[8]=$x.$b;
		$title[9]=$x.$c;
		$title[10]=$y.$a;
		$title[11]=$y.$b;
		$title[12]=$y.$c;
		$title[13]=$z.$a;
		$title[14]=$z.$b;
		$title[15]=$z.$c;
	}



	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_num = 0 ;
	$_tot = 0 ;	//装车数
	$_sum_time=0; //装车总时
	$_sum_time_fz=0; //辅助总时
	$_sum_jszc=0; //计时车数
	$_sum_all=0; //合计车数
	$_sum_time_all=0;//总工时
	if (!empty($rtn)){
		foreach($rtn as $key=>$cv){
			$_all = 0 ; //单位总车数
			$a1=$cv['a_1']?$cv['a_1']:0;
			$_all +=$a1;
			$a2=$cv['a_2']?$cv['a_2']:0;
			$_all +=$a2;
			$a3=$cv['a_3']?$cv['a_3']:0;
			$_all +=$a3;
			$b1=$cv['b_1']?$cv['b_1']:0;
			$_all +=$b1;
			$b2=$cv['b_2']?$cv['b_2']:0;
			$_all +=$b2;
			$b3=$cv['b_3']?$cv['b_3']:0;
			$_all +=$b3;
			$c1=$cv['c_1']?$cv['c_1']:0;
			$_all +=$c1;
			$c2=$cv['c_2']?$cv['c_2']:0;
			$_all +=$c2;
			$c3=$cv['c_3']?$cv['c_3']:0;
			$_all +=$c3;
			$gcnum=$cv['gcnum']?$cv['gcnum']:0;
			$_tot +=$_all;
			$al_time=secToTime($cv['altime']);
			$_sum_time+=$cv['altime'];

			//工时/车
			$per_time_car=($cv['altime']/60/$_all);
			$per_time_car=$per_time_car?sprintf("%.1f",$per_time_car):0;
			//车/工时
			$per_car_time=sprintf("%.1f",(60/$per_time_car));
			$per_car_time=$per_car_time?$per_car_time:1;
			//辅助计时
			$ctime=$ctime_ary[$key];
			$_sum_time_fz+=$ctime;
			$ctime_str=$ctime?secToTime($ctime):'';
			//总工时
			if(empty($ctime_str)){
				$al_time_str=$al_time;
			}else{
				unset($ctime_ary[$key]);
				$al_time_str=secToTime($cv['altime']+$ctime);
			}

			$_sum_time_all+=($cv['altime']+$ctime);
			//计时车数
			$_sum_jszc+=$gcnum;
			$_sum_all+=($gcnum+$_all);

			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:num>'.($_num+1).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['car_type'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['downer'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['jsno'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['dkno'].'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($_all).'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$al_time.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$ctime_str.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$gcnum.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.($gcnum+$_all).'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$al_time_str.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$per_time_car.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$per_car_time.'</td>';
			$html .= '<td width=”100″ align=right x:str></td>';
			$html .= '<td width=”100″ align=right x:str></td>';
			$html .= '</tr>' ;
			$_num++;
		}
	}


	//补充只有辅助计时的记录
	if($ctime_ary){
		foreach($ctime_ary as $key=>$vo){
			$cv=$ctime_rtn[$key];
			$a1=$a2=$a3=$b1=$b2=$b3=$c1=$c2=$c3=0;
			$_all=0;
			$al_time=0;
			$_sum_time_fz+=$vo;
			$_sum_time_all+=$vo;
			$al_time_str=$ctime_str=secToTime($vo);
			$per_car_time=$per_time_car=0;
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:num>'.($_num+1).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['car_type'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['downer'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['jsno'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['dkno'].'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c1).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c2).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($a3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($b3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($c3).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($_all).'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$al_time.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$ctime_str.'</td>';
			$html .= '<td width=”100″ align=right x:num>0</td>';
			$html .= '<td width=”100″ align=right x:num>0</td>';
			$html .= '<td width=”100″ align=right x:num>'.$al_time_str.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$per_time_car.'</td>';
			$html .= '<td width=”100″ align=right x:num>'.$per_car_time.'</td>';
			$html .= '<td width=”100″ align=right x:str></td>';
			$html .= '<td width=”100″ align=right x:str></td>';
			$html .= '</tr>' ;
			$_num++;
		}
	}

	$html .= '<tr><td colspan="16" align=right>合计</td><td align="right">'.$_tot.'</td><td align="right">'.secToTime($_sum_time).'</td><td align="right">'.secToTime($_sum_time_fz).'</td>';
	$html .= '<td align="right">'.$_sum_jszc.'</td><td align="right">'.$_sum_all.'</td><td align="right">'.secToTime($_sum_time_all).'</td><td colspan="4"></td></tr>';
	$html .= '</body></html></table>';
	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}



/**
 * 导出装车登记记录信息
 * @author leiqianyong 2015-10-26 9:44 AM
 */
function device_act_improt_out_all(){
	global $device_obj,$project_obj;
	$data = $_GET;

	//1装车2计时3渣车
	$type=request('type',false,true,false,1);
	$list=$device_obj->get_report_all($data,$type);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}

	//项目工时间隔
	$pinfo=$project_obj->info($data['pid'],'time_zc');
	$time_zc=($pinfo && $pinfo['time_zc']>0)?$pinfo['time_zc']:300;


	$filename = "SYT_REPORT:".date('YmdHis') ;
	if(1==$type){
	   $title_ext = '挖机装车登记信息表' ;
	   $title = array('设备号','车牌号','登记时间','间隔秒数','工作类型','合法性','渣车类型','土石类型','机手号','机手姓名','地块号','地块名称','照片名称','登记编码','项目名称');
	}elseif(3==$type){
		$title_ext = '渣车装车登记信息表' ;
		$title = array('设备号','车牌号','车辆型号','登记时间','位置','工作类型','合法性','挖机号','挖机型号','线路','距离','司机编号','司机姓名','运距编号','照片名称','登记编码','项目名称');
	}else{
		echo 'Param Error !';
		exit;
	}
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if(3==$type && ($data['start_time'] || $data['end_time'])){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}



	//项目名称
	$project_ary=$project_obj->project_all();

	$type_ary=array(1=>'装车',2=>'计时',3=>'渣车');
	$state_ary=array(1=>'有效',0=>'无效');
	if(1==$type){
	$dust_ary=array(1=>'Ⅰ',2=>'Ⅱ',3=>'Ⅲ');
		if (!empty($list)){
			$_header=0;
			$length=0;
			foreach($list as $key=>$cv){
				if($_header){
					$length=$cv['ctime']-$_header;
				}
				$_header=$cv['ctime'];
				$dis_time=($length>900||$length<90)?$time_zc:$length;

				$html .= '<tr>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['dno']).'</td>' ;
				$html .= '<td width=”100″ align=center x:str><strong>'.($cv['dname']).'</strong></td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(date('YmdHis',$cv['ctime'])).'</td>' ;
				$html .= '<td width=”100″ align=center x:num>'.($dis_time).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($type_ary[$cv['type']]).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($state_ary[$cv['state']]).'</td>' ;
				if(0==$cv['gcat']) {
					$html .= '<td width=”100″ align=center x:str>' . (strtoupper($cv['truck'])) . '</td>';
					$html .= '<td width=”100″ align=center x:str>' . ($dust_ary[$cv['dust']]) . '</td>';
				}else{
					$html .= '<td width=”100″ align=center x:str></td>';
					$html .= '<td width=”100″ align=center x:str></td>';
				}
				$html .= '<td width=”100″ align=left x:str>'.($cv['jsno']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['doper']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['dkno']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['dkname']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['img']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['code']).'</td>' ;
				$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
				$html .= '</tr>' ;
			}
		}
	}elseif(3==$type){
		$posi_ary=array(1=>'取土点',2=>'卸土点');
		if (!empty($list)){
			foreach($list as $key=>$cv){
				$html .= '<tr>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['dno']).'</td>' ;
				$html .= '<td width=”100″ align=center x:str><strong>'.($cv['dname']).'</strong></td>' ;
				$html .= '<td width=”100″ align=center x:str><strong>'.($cv['car_type']).'</strong></td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(date('YmdHis',$cv['ctime'])).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($posi_ary[$cv['posi']]).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($type_ary[$cv['type']]).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($state_ary[$cv['state']]).'</td>' ;
				/**
				$html .= '<td width=”100″ align=center x:str>'.(1==$cv['posi']?$cv['wj_no']:'').'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(1==$cv['posi']?$cv['wj_xh']:'').'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(2==$cv['posi']?$cv['line']:'').'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(2==$cv['posi']?$cv['line_dis']:'').'</td>' ;
				**/
				$html .= '<td width=”100″ align=center x:str>'.($cv['wj_no']).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['wj_xh']).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['line']).'</td>' ;
				//$html .= '<td width=”100″ align=center x:str>'.($cv['line_dis']).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>'.(_parseMail($cv['dis'])).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['sjno']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['doper']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['yunno']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['img']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str>'.($cv['code']).'</td>' ;
				$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
				$html .= '</tr>' ;
			}
		}

		$html .= '<tr><td colspan="4">统计：</td><td colspan="4">复核：</td><td colspan="3">审核：</td></tr>';
	}

	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}

//里程格式转换
function _parseMail($sum){
	return $sum<1000?"<b>".$sum."</b>".' 米':"<b>".sprintf("%.2f",$sum/1000)."</b>".' 公里';
}


/**
 * 渣车模式报表导出
 * @author leiqianyong 2016-3-16 16:58 PM
 */
function device_act_improt_car_out(){
	global $device_obj,$project_obj;
	$data = $_GET;

	$type=$data['types'];
	//1-挖机明细  2-运输路线信息 3-渣车明细 4-挖机统计表 5-挖机装车表 6-运输编号统计
	$allow_ary=array(1,2,3,4,5,6);
	if(!is_numeric($type) || !in_array($type, $allow_ary)){
		exit('Error Params !');
	}

	//1-挖机明细
	if(1==$type){
		$list=$device_obj->get_wj_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_wj_out($list,$data);
		exit;
	}

	//2-运输路线信息
	if(2==$type){
		$list=$device_obj->get_line_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_line_out($list,$data);
		exit;
	}

	//3-运输路线信息
	if(3==$type){
		$list=$device_obj->get_car_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_car_out($list,$data);
		exit;
	}

	//4-渣车装车统计表
	if(4==$type){
		$list=$device_obj->_get_car_total_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_car_total_out($list,$data);
		exit;
	}


	//6-运距编号统计表
	if(6==$type){
		$list=$device_obj->_get_car_total_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_car_total_out($list,$data,1);
		exit;
	}


	//5-挖机装车统计表
	if(5==$type){
		$list=$device_obj->get_wj_total_report($data);
		if(empty($list)){
			echo 'No Data !' ;
			exit;
		}
		device_report_wj_total_out($list,$data);
		exit;
	}

}

//挖机明细
function device_report_wj_out($list,$data){
	$title_ext = '挖机装车登记明细表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('挖机号','挖机型号','渣车牌号','车辆型号','登记时间','项目名称');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	//项目名称
	global $project_obj;
	$project_ary=$project_obj->project_all();

		$wj='';
		$num=0;
		$_num=count($list);
		$_no=1;
		foreach($list as $key=>$cv){
			if($cv['wj_no']==$wj || ''==$wj){
				$html .= '<tr>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['wj_no']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['wj_xh']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['car_type']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
				$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
				$html .= '</tr>' ;
				$num++;
				$wj=$cv['wj_no'];
			}elseif($wj){
				$html .= '<tr>';
				$html .= '<td width=”100″ align=center x:str>小计</td>' ;
				$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '</tr>';
				$num=0;
				$html .= '<tr>' ;
				$html .= '<td width=”100″ align=center x:str>'.($cv['wj_no']).'</td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['wj_xh']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str><strong>'.($cv['car_type']).'</strong></td>' ;
				$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
				$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
				$html .= '</tr>' ;
				$num++;
				$wj=$cv['wj_no'];
			}

			//最后一个
			if($_no==$_num){
				$html .= '<tr>';
				$html .= '<td width=”100″ align=center x:str>小计</td>' ;
				$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
				$html .= '</tr>';
			}

			$_no++;
		}

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


//线路明细
function device_report_line_out($list,$data){
	$title_ext = '运输路线登记信息表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('路线号','渣车牌号','渣车型号','登记时间','项目名称');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	//项目名称
	global $project_obj;
	$project_ary=$project_obj->project_all();

	$wj='';
	$num=0;
	$_num=count($list);
	$_no=1;
	foreach($list as $key=>$cv){
		if($cv['line']==$wj || ''==$wj){
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['line']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['car_type']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
			$html .= '</tr>' ;
			$num++;
			$wj=$cv['line'];
		}elseif($wj){
			$html .= '<tr>';
			$html .= '<td width=”100″ align=center x:str>小计</td>' ;
			$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '</tr>';
			$num=0;
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['line']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['car_type']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
			$html .= '</tr>' ;
			$num++;
			$wj=$cv['line'];
		}

		//最后一个
		if($_no==$_num){
			$html .= '<tr>';
			$html .= '<td width=”100″ align=center x:str>小计</td>' ;
			$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '</tr>';
		}

		$_no++;
	}

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


//线路明细
function device_report_car_out($list,$data){
	$title_ext = '渣车运输登记明细表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('渣车牌号','车辆型号','线路号','登记时间','项目名称');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	//项目名称
	global $project_obj;
	$project_ary=$project_obj->project_all();

	$wj='';
	$num=0;
	$_num=count($list);
	$_no=1;
	foreach($list as $key=>$cv){
		if($cv['dname']==$wj || ''==$wj){
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['dname']).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['car_type']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['line']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
			$html .= '</tr>' ;
			$num++;
			$wj=$cv['dname'];
		}elseif($wj){
			$html .= '<tr>';
			$html .= '<td width=”100″ align=center x:str>小计</td>' ;
			$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '</tr>';
			$num=0;
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['dname']).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['car_type']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['line']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.(date('YmdHis',$cv['time'])).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
			$html .= '</tr>' ;
			$num++;
			$wj=$cv['dname'];
		}

		//最后一个
		if($_no==$_num){
			$html .= '<tr>';
			$html .= '<td width=”100″ align=center x:str>小计</td>' ;
			$html .= '<td width=”100″ align=center x:num>'.($num).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '<td width=”100″ align=center x:str>&nbsp;</td>' ;
			$html .= '</tr>';
		}

		$_no++;
	}

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


//渣车统计表
function device_report_car_total_out($list,$data,$isyunj=0){
	if($isyunj){
		$title_ext = '运距编号统计表';
	}else {
		$title_ext = '渣车运输统计表';
	}
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	//从总记录中统计数据
	foreach($list as $cv){
		if($isyunj){
			$dname = $cv['dname'] . '_' . $cv['yunno'] . '_' . $cv['sjno'];
		}else {
			$dname = $cv['dname'] . '_' . $cv['line'] . '_' . $cv['sjno'];
		}

	   	$rtn[$dname]['dname']=$cv['dname'];
	   	$rtn[$dname]['car_type']=$cv['car_type'];
	   	$rtn[$dname]['sjno']=$cv['sjno'];
	   	$rtn[$dname]['doper']=$cv['doper'];
	   	$rtn[$dname]['line']=$cv['line'];
		$rtn[$dname]['yunno']=$cv['yunno'];
	   	$rtn[$dname]['sum']=($rtn[$dname]['sum']?$rtn[$dname]['sum']+1:1);
	   	if(empty($rtn[$dname]['start']))$rtn[$dname]['start']=$cv['ctime'];
	   	$rtn[$dname]['end']=$cv['ctime'];
	}

    if($isyunj) {
		$title = array('渣车牌号', '车辆型号', '司机编号', '司机姓名', '运距编号', '车数', '首车登记时间', '尾车登记时间', '司机签字');
	}else{
		$title = array('渣车牌号', '车辆型号', '司机编号', '司机姓名', '线路', '车数', '首车登记时间', '尾车登记时间', '司机签字');
	}
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_sum=0;
	foreach($rtn as $key=>$cv){
		$html .= '<tr>' ;
		$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
		$html .= '<td width=”100″ align=left x:str><strong>'.($cv['car_type']).'</strong></td>' ;
		$html .= '<td width=”100″ align=left x:str>'.$cv['sjno'].'</td>' ;
		$html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
		if($isyunj) {
			$html .= '<td width=”100″ align=left x:str><strong>' . ($cv['yunno']) . '</strong></td>';
		}else{
			$html .= '<td width=”100″ align=left x:str><strong>' . ($cv['line']) . '</strong></td>';
		}
		$html .= '<td width=”100″ align=left x:num>'.$cv['sum'].'</td>' ;
		$html .= '<td width=”100″ align=right x:str>'.date('H:i',$cv['start']).'</td>' ;
		$html .= '<td width=”100″ align=right x:str>'.date('H:i',$cv['end']).'</td>' ;
		$html .= '<td width=”100″ align=right x:str></td>' ;
		$html .= '</tr>' ;
		$_sum+=$cv['sum'];
	}

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>合计：&nbsp;&nbsp;'.$_sum.'</td></tr>';
	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


//渣车统计表
function device_report_wj_total_out($list,$data){
	$title_ext = '挖机装车统计表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('挖机牌号','挖机型号','车辆型号','车数','司机签字');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_sum=0;
	foreach($list as $key=>$cv){
		$html .= '<tr>' ;
		$html .= '<td width=”100″ align=left x:str><strong>'.($cv['wj_no']).'</strong></td>' ;
		$html .= '<td width=”100″ align=left x:str><strong>'.($cv['wj_xh']).'</strong></td>' ;
		$html .= '<td width=”100″ align=left x:str><strong>'.($cv['car_type']).'</strong></td>' ;
		$html .= '<td width=”100″ align=left x:num>'.$cv['sum'].'</td>' ;
		$html .= '<td width=”100″ align=right x:str></td>' ;
		$html .= '</tr>' ;
		$_sum+=$cv['sum'];
	}

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>合计：&nbsp;&nbsp;'.$_sum.'</td></tr>';
	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


/**
 * 导出装车登记记录信息
 * @author leiqianyong 2015-10-26 9:43 AM
 */
function device_act_time_out(){
	global $device_obj,$project_obj;
	$data = $_GET;

	$list=$device_obj->get_time_report($data);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}


	$time_project=$project_obj->getProjectTime();


	$title_ext = '挖机计时统计信息表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	$title = array('序号','名称','产权人','次数','时间(分)');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_num=0;
	if (!empty($list)){
		foreach($list as $key=>$cv){

			$ptime=$time_project[$cv['pid']];
			$ptime=$ptime>0?$ptime:3;
			$time_sum=$cv['number']*$ptime;

			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:num>'.($_num+1).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['downer'].'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($cv['times']).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.$time_sum.'</td>' ;
			$html .= '</tr>' ;
			$_num++;
		}
	}


	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


/**
 * 导出计时登记记录信息
 * @author leiqianyong 2015-10-26 9:44 AM
 */
function device_act_improt_time_all(){
	global $device_obj,$project_obj;
	$data = $_GET;

	$list=$device_obj->get_time_report_all($data);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}


	$title_ext = '挖机装车辅助登记工作报表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;

	$title = array('设备号','车牌号','登记时间','工作类型','合法性','','','照片名称','登记编码','项目名称');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	//项目名称
	$project_ary=$project_obj->project_all();
	$type_ary=array(1=>'装车',2=>'计时');
	$state_ary=array(1=>'有效',0=>'无效');
	if (!empty($list)){
		foreach($list as $key=>$cv){
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['dno']).'</td>' ;
			$html .= '<td width=”100″ align=center x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=center x:str>'.(date('YmdHis',$cv['ctime'])).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($type_ary[$cv['type']]).'</td>' ;
			$html .= '<td width=”100″ align=center x:str>'.($state_ary[$cv['state']]).'</td>' ;
			$html .= '<td width=”100″ align=center x:str></td>' ;
			$html .= '<td width=”100″ align=center x:str></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.($cv['img']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.($cv['code']).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($project_ary[$cv['pid']]).'</td>' ;
			$html .= '</tr>' ;
		}
	}

	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}


function device_act_unique(){
	global $device_obj;
	$data['no']=request('param');
	$s=$device_obj->device_info_by_no($data['no']);
	if($s){
		exit('该设备号已存在');
	}
	exit('y');
}
/**
* 添加设备号
*
*/
function device_act_add(){
	global $device_obj,$admin_obj;
	$_userinfo=$admin_obj->out_cookie();
	if(!$_userinfo['id']){
		echo_json(array('state'=>false,'msg'=>1017));
	}
	$data['no']=request('no');

	if(!$data['no']){
		echo_json(array('state'=>false,'msg'=>1436));
	}

	$data['master']=request('master',false,true,false,0);
	$data['own']=request('own',false,true,false,0);

	if($_userinfo['group']!=6){
	    $data['master']= $_userinfo['id'];
	    $data['own']= 1;
	}

	$data['id']=request('id',false,true);
	$now_time=time();

	//添加时不能有
	if(!$data['id']){
		$s=$device_obj->device_info_by_no($data['no']);
		if($s)echo_json(array('state'=>false,'msg'=>1437));
		$data['time']=$now_time;
	}

	//修改时 重复判断
	if($data['id']){
		//判断设备号名是否修改
		$dinfo=$device_obj->get_device_info($data['id']);
		//如果设备编号修改
		if($dinfo['no']!=$data['no']){
			//修改编号时UID必须是总管理
			if(1!=$_userinfo['id']){
				echo_json(array('state'=>false,'msg'=>1017));
			}

			//判断修改的设备是否重复
			if($device_obj->device_info_by_no($data['no'])){
				echo_json(array('state'=>false,'msg'=>1437));
			}

			$data['update']=$now_time;
			//记录日志
			$_rtn['time']=date('Y-m-d H:i:s',$now_time);
			$_rtn['no']=$dinfo['no'].'->'.$data['no'];
			$_rtn['uid']=$_userinfo['id'];
			$_rtn['ip']=get_remote_ip();
			$_rtn['request']=$data;
			$device_obj->recordlog(var_export($_rtn,true),'_nochange.txt');
		}
    }

	$s = $device_obj->ins_up_data($data);
	if($data['id']){
		if(!$s){
			echo_json(array('state'=>false,'msg'=>1019));
		}
		echo_json(array('state'=>true,'msg'=>1018));
	}
	if(!$s){
		echo_json(array('state'=>false,'msg'=>1016));
	}
	echo_json(array('state'=>true,'msg'=>1015));
}


/**
 * 登录系统后获取挖机编号和线路编号接口
 * @author leiqianyong 2016-3-12 15:04 PM
 */
function device_act_getparam(){
	global $device_obj, $project_obj;

	$rtn = array('state'=>false);
	//得到设备信息
	$no = request('no');    //设备号
	//得到设备信息
	$device_info = $device_obj->device_info_by_no($no);
	if(!$device_info){
		$rtn['code'] = 1104;    //设备不存在
		api_echo_json($rtn);
	}
	$bind_list = $device_obj->bind_list($device_info['id'], array('state'=>1));
	if(!$bind_list){
		$rtn['code'] = 1105;    //设备没有指定项目
		api_echo_json($rtn);
	}
	if(count($bind_list) > 1){
		$rtn['code'] = 1106;    //设备绑定了多个项目
		api_echo_json($rtn);
	}
	$bind_info = $bind_list[0];
	//得到绑定的项目信息
	$project_info = $project_obj->info($bind_info['pid']);
	if($project_info['state'] == 2){
		$rtn['code'] = 1201;    //项目已关闭
		api_echo_json($rtn);
	}

	$result=$device_obj->setinfo_get($project_info['uid']);
	if(empty($result)){
		$rtn['code'] = 1202;
		api_echo_json($rtn);
	}

	foreach($result as $val){
		$_ary[$val['type']][]=$val['code'];
	}

	if(empty($_ary[1])){
		$rtn['code'] = 1203;  //挖机编号未设置
		api_echo_json($rtn);
	}

	if(empty($_ary[2])){
		$rtn['code'] = 1204;  //线路编号未设置
		api_echo_json($rtn);
	}
	$res['car']=$_ary[1];
	$res['line']=$_ary[2];
	api_echo_json($res);
}



/**
* 登记作业点接口
* @author zhufu 2014-06-06
*
*/
function device_act_checkin(){
	global $device_obj, $project_obj;

	$rtn = array('state'=>false);
        $time=$device_obj->getTime();
	if(!$time || $time<=0){
	  $rtn['code'] = 1110;
	  api_echo_json($rtn);
	}

	$autoresult = $device_obj->auto_check('checkin');
	if(is_numeric($autoresult)){
		$rtn['code'] = $autoresult;
		api_echo_json($rtn);
	}
	if(!$_FILES['img']){
		$rtn['code'] = 1107;    //没有上传登记图片
		api_echo_json($rtn);
	}

	$no = request('no');    //设备号
	$ctime = request('ctime');  //设备取点时间
	$ctime = is_numeric($ctime) ? $ctime : strtotime($ctime);   //兼容客户端上传时间格式字符串
	$coord_x = request('coord_x');  //取点地点的坐标经度
	$coord_y = request('coord_y');  //取点地点的坐标纬度
	$type=request('type',false,true);

	//得到设备信息
	$device_info = $device_obj->device_info_by_no($no);
	if(!$device_info){
		$rtn['code'] = 1104;    //设备不存在
		api_echo_json($rtn);
	}
	$bind_list = $device_obj->bind_list($device_info['id'], array('state'=>1));
	if(!$bind_list){
		$rtn['code'] = 1105;    //设备没有指定项目
		api_echo_json($rtn);
	}
	if(count($bind_list) > 1){
		$rtn['code'] = 1106;    //设备绑定了多个项目
		api_echo_json($rtn);
	}
	$bind_info = $bind_list[0];
	//得到绑定的项目信息
	$project_info = $project_obj->info($bind_info['pid']);
	if($project_info['state'] == 2){
		$rtn['code'] = 1201;    //项目已关闭
		api_echo_json($rtn);
	}

	//处理上传的图片
	$image = $device_obj->save_img('img','item/'.$project_info['id']);
	if(!$image){
		$rtn['code'] = 1108;    //登记图片保存失败
		api_echo_json($rtn);
	}
	$img = strstr($image['folder'], '/upload') . $image['name'];
	$str = $_FILES['img']['name'];

	$app_time = strtotime(str_ireplace("_",'',substr(trim($str),0,strripos($str,'.'))));

	$img_state = $device_obj->device_unique_img($app_time);
	if($img_state){//当有重复提交的时候不再进行入库处理

		$s = $device_obj->api_logs(array(
			'ctime'=>date('Y-m-d H:i:s',$ctime/1000),
			'time'=>date('Y-m-d H:i:s',NOW),
			'app_img_name'=>$str,
			'databases_img_name'=>$app_time,
			'exist_id'=>$img_state['id'],
		));//日志
		$rtn = array('state'=>true, 'list'=>true);    //登记成功
		api_echo_json($rtn);
	}
	$device_obj->api_logs($str.' '.$app_time.' 运输类型：'.$type);
	$r = $device_obj->add_checkin($bind_info['pid'], $bind_info['did'], $coord_x, $coord_y, $img, $ctime,$type,$app_time);
	//$strs = $bind_info['pid'].'__'.$bind_info['did'].'__'.$coord_x.'__'.$coord_y.'__'.$img.'__'.$ctime.'__'.$type.'__'.$app_time ;
	//error_log($strs."\r\n", 3,'log.txt');
	if(!$r){
		$rtn['code'] = 1109;    //登记失败
		api_echo_json($rtn);
	}
	$device_obj->chTime();
	$rtn = array('state'=>true, 'list'=>true);    //登记成功

	api_echo_json($rtn);


}


/**
 * 渣车设备上传资料接口（登记作业点接口）
 * @author leiqianyong 2016-3-12 15:54 PM
 */
function device_act_carrecord(){

	device_act_record();
}




/**
 * 登记作业点接口
 * @author leiqianyong 2015-10-9 15:12 PM
 */
function device_act_record(){
	global $device_obj, $project_obj;

	//错误输入
	$no=request('no');
	if(empty($no)){
           $rtn = array('state'=>false, 'list'=>true);
           $_log['time']=date("Y-m-d H:i:s");
           $_log['msg']=$_REQUEST;
	       $device_obj->recordlog(var_export($_log,true),'_err.txt');
     	   $rtn['code'] = 1107;
	       api_echo_json($rtn);
	       exit;
	}

	$rtn = array('state'=>false);
	$autoresult = $device_obj->auto_check('checkin');
	if(is_numeric($autoresult)){
		$rtn['code'] = $autoresult;
		api_echo_json($rtn);
	}
	if(!$_FILES['img']){
		$rtn['code'] = 1107;    //没有上传登记图片
		api_echo_json($rtn);
	}

	$no = request('no');    //设备号
	$no=str_replace("-","",$no);
	$ctimestr = $ctime = request('ctime');  //设备取点时间
	$ctime = is_numeric($ctime) ? $ctime : strtotime($ctime);   //兼容客户端上传时间格式字符串
	$coord_x = request('coord_x');  //取点地点的坐标经度
	$coord_y = request('coord_y');  //取点地点的坐标纬度
	//挖机模式  1装车模式 2计时模式  3渣车模式
	$type=request('type',false,true,false,3);

	$wj_no=request('car');  //挖机编号
	$wj_no=empty($wj_no)?'':$wj_no;
	$line=request('line'); //线路编号
	$line=empty($line)?'':$line;
	$posi=request('posi',false,true,false,0);  //位置
	$line_dis=''; //线路距离
	$wj_xh='';//挖机型号
	$car_type='';//渣车型号

	//司机，机手，地块
	//司机
	$sjno=request('sjno');
	$extend['sjno']=$sjno?trim($sjno):'';
	//机手
	$jsno=request('jsno');
	$extend['jsno']=$jsno?trim($jsno):'';
	//地块
	$dkno=request('dkno');
	$extend['dkno']=$dkno?trim($dkno):'';

	//装车模式
	$truck=0;
	$dust=0;
	$onload=0;
	if(1==$type){
		//渣车类型 a.b.c
		$truck=request('truck');
		$truck_ary=array(1=>'a',2=>'b',3=>'c');
		//土石类型  1,2,3
		$dust=request('dust');
		if(!$truck || !$dust){
			$rtn['code']=1111;   //缺少装车参数
			api_echo_json($rtn);
		}
		$truck=$truck_ary[$truck];
		//渣车装载类型   1-空车   2-重车
		$onload=request('onload',false,true,false,1);
	}

	//不饱和模式
	$gcat=request('gcat',false,true,false,0);

	//得到设备信息
	$device_info = $device_obj->device_info_by_no($no);
	if(!$device_info){
		$rtn['code'] = 1104;    //设备不存在
		api_echo_json($rtn);
	}
	$bind_list = $device_obj->bind_list($device_info['id'], array('state'=>1));
	if(!$bind_list){
		$rtn['code'] = 1105;    //设备没有指定项目
		api_echo_json($rtn);
	}
	if(count($bind_list) > 1){
		$rtn['code'] = 1106;    //设备绑定了多个项目
		api_echo_json($rtn);
	}
	$bind_info = $bind_list[0];
	//得到绑定的项目信息
	$project_info = $project_obj->info($bind_info['pid']);
	if($project_info['state'] == 2){
		$rtn['code'] = 1201;    //项目已关闭
		api_echo_json($rtn);
	}
    $xid=$project_info['uid'];
    $admin_obj=new admin();
    $times=$admin_obj->getTime($xid);
    if(!$times || $times<=0){
        $rtn['code'] = 1110;
        api_echo_json($rtn);
    }

    //图片命名
    if($type!=3){
      $dust_ary=array(1=>'Ⅰ',2=>'Ⅱ',3=>'Ⅲ');
	  $picname = $bind_info['car_number'].'_'.date('YmdHis',$ctime).'_'.(1==$type?'ZC_'.strtoupper($truck).'_'.$dust.'_'.(1==$onload?'K':'Z'):'js') ;
    }else{
      $code=1==$posi?$wj_no:$line;
      $_wjinfo=$device_obj->getCarset($xid, $posi, $code);
      if(!empty($_wjinfo)){
      if(1==$posi){
      	$wj_xh=$_wjinfo['comment'];
      }else{
      	$line_dis=$_wjinfo['comment'];
      }
      }
      $picname = $bind_info['car_number'].'_'.date('YmdHis',$ctime) ;
    }
    $car_type=$bind_info['cartype'];

    //处理上传的图片
	$image = $device_obj->save_img('img','item/'.$project_info['id'],$picname);
	if(!$image){
		$rtn['code'] = 1108;    //登记图片保存失败
		api_echo_json($rtn);
	}
	$img = strstr($image['folder'], '/upload') . $image['name'];
	$str = $_FILES['img']['name'];

    //前后上传的图片名称也可能出现相同的情况 @author leiqianyong 2021-4-10 23:16:PM
	//$app_time = strtotime(str_ireplace("_",'',substr(trim($str),0,strripos($str,'.'))));
	$app_time = $ctime;

	$img_state = $device_obj->device_unique_img($app_time,$device_info['id']);
	if($img_state){//当有重复提交的时候不再进行入库处理
		$s = $device_obj->api_logs(array(
				'ctime'=>date('Y-m-d H:i:s',$ctime/1000),
				'time'=>date('Y-m-d H:i:s',NOW),
				'app_img_name'=>$str,
				'databases_img_name'=>$app_time,
				'exist_id'=>$img_state['id'],
		));//日志
		$rtn = array('state'=>true, 'list'=>true);    //登记成功
		api_echo_json($rtn);
	}

	$dname=$bind_info['car_number'];  //设备名称
	$downer=$bind_info['linkman'];    //机车产权人

	//获取机手、地块、司机 其他信息
	if($extend['sjno'] || $extend['dkno'] || $extend['wjno']){
		$duinfo=$device_obj->getUserinfo($extend,$xid);
		$doper='';
		if(3==$type){
			if($duinfo['sj']){
				$doper=$duinfo['sj']['name'];
				$extend['dophone']=$duinfo['sj']['phone'];
			}
		}else{
			if($duinfo['js']){
				$doper=$duinfo['js']['name'];
				$extend['dophone']=$duinfo['js']['phone'];
			}

			if($duinfo['dk']){
				$extend['dkname']=$duinfo['dk']['name'];
			}
		}
	}else{
	   $doper=$bind_info['doper'];
	}


	//计时模式
	$group=0;
	if(2==$type){
	    $group = $device_obj->getlast($bind_info['pid'], $bind_info['did'],$ctime);
	}
	$r = $device_obj->add_checkin($bind_info['pid'], $bind_info['did'], $coord_x, $coord_y, $img, $ctime,$type,$app_time,$truck,$dust,$dname,$downer,$doper,$no,$onload,$xid,$group,$picname,$wj_no,$line,$posi,$wj_xh,$line_dis,$car_type,$extend,$gcat);

	if(!$r){
		$rtn['code'] = 1109;    //登记失败
		api_echo_json($rtn);
	}
	//渣车模式       坐标提取   数据库减压
	if(3==$type){

	    $packet=request('packet');   //数据库性能问题处理
	    if($packet){
	    	$packet=trim(str_replace("\\", "", $packet),'"');
	    	$ary=json_decode($packet,true);
	    	//添加图片上的坐标
	    	if($coord_x>70)$ary[]=array('longitude'=>$coord_x,'latitude'=>$coord_y,'createTime'=>$ctimestr);
	    	if($ary){


                coord_data_bat($ary,$r,$ctime,$xid);
	    	}
	    }
	}

	$admin_obj->chTime($xid);
	$rtn = array('state'=>true, 'list'=>true);    //登记成功

	$_log['wno']=$_REQUEST['no'];
	$_log['time']=time();
	$_log['ctime']=$_REQUEST['ctime'];
	$_log['code']=$picname;
	$_log['coord_x']=$coord_x;
	$_log['coord_y']=$coord_y;
	$device_obj->recordlog(var_export($_log,true));
	api_echo_json($rtn);
}


// 以数据记录方式插入 @author leiqianyong 2018-4-18 9:19 AM
/**
function coord_data_bat($ary,$r,$ctime){
	$rtn=coordClear($ary,$ctime);
	if(empty($rtn))return false;

	//批量添加坐标信息
	$sql="";
	$dis_sum=0;
	foreach($rtn as $val){
		$timestr=$val['createTime'];
		$time=strtotime($val['createTime']);
		$endX=$val['longitude'];
		$endY=$val['latitude'];
		$dis=$val['dis'];
		$dis_sum+=$dis;
		$disall=$val['disall'];
		$sql .=",({$r},{$time},'{$timestr}','{$endX}',{$endY},{$dis},{$disall})";
	}
	$sql="INSERT INTO syt_maps (`pid`,`time`,`timestr`,`coord_x`,`coord_y`,`dis`,`disall`) VALUES ".trim($sql,',');

	global $device_obj;
	$device_obj->device_insert_bat($sql);

	if($dis_sum>0){
		$updatedata['disall']=$dis_sum;
		$updatedata['dis']=$dis_sum;
		$device_obj->update_checkin($r,$updatedata);
	}
}
**/


//将坐标作为文本存储到数据库中
function coord_data_bat($ary,$r,$ctime,$xid=0){
	$rtn=coordClear($ary,$ctime);
	if(empty($rtn))return false;

	//批量添加坐标信息
	$dis_sum=0;
	$i=1;
	foreach($rtn as $key=>$val){
		$data['id']=$i;
		$i++;
		$data['time']=strtotime($val['createTime']);
		$data['coord_x']=$val['longitude'];
		$data['coord_y']=$val['latitude'];
		$data['timestr']=$val['createTime'];
		$data['dis']=$val['dis'];
		$dis_sum+=$val['dis'];
		$disall=$val['disall'];
		$crtn[]=$data;
	}

	$coord=addslashes(var_export($crtn, TRUE));
	$sql="INSERT INTO syt_mapstr (`pid`,`coord`,`dis`) VALUES ({$r},'{$coord}',{$dis_sum})";
	global $device_obj;
	$device_obj->device_insert_bat($sql);


	if($dis_sum>0){
		//根据里程自动判断当前的运距编号
		$rtn_line =$device_obj->get_set_line_byuid($xid);
		if($rtn_line){
			foreach($rtn_line as $vo){
				if($dis_sum>=$vo['yun_min'] && $dis_sum<=$vo['yun_max']){
					$updatedata['yunno'] = $vo['code'];
					break;
				}
			}
		}

		$updatedata['disall']=$dis_sum;
		$updatedata['dis']=$dis_sum;
		$device_obj->update_checkin($r,$updatedata);
	}
}



function coord_data($ary,$r){
	global $device_obj;
	$startX=0;
	$startY=0;
	$endX=0;
	$endY=0;
	$dis_sum=0;
	$dis_all=0;
	$mapobj=new maps();
	foreach($ary as $key=>$val){
		if($val['longitude']<0||$val['latitude']<0)continue;
		$data['timestr']=$val['createTime'];
		$data['time']=strtotime($val['createTime']);
		$endX=$data['coord_x']=$val['longitude'];
		$endY=$data['coord_y']=$val['latitude'];
		$data['pid']=$r;

		if($startX==0){
			$data['dis']=0;
		}else{
			$_dis=getDistance($startX,$startY,$endX,$endY);//计算距离
			$dis_all+=$data['disall']=$_dis;     //两点间时间距离
			$_dis=($_dis>400)?0:$_dis;           //筛选，超过范围的省略
			$dis_sum+=$data['dis']=$_dis;
		}

		$mapobj->maps_add($data);

		$startX=$val['longitude'];
		$startY=$val['latitude'];
	}
	if($dis_sum>0){
		$updatedata['disall']=$dis_all;
		$updatedata['dis']=$dis_sum;
		$device_obj->update_checkin($r,$updatedata);
	}
}


/**
* ajax 获取设备列表
* @author xiaxin 2014-06-10
*
*/
function device_act_ajax_list(){
	global $device_obj;
	$title = request('device_no');
	$state = 1;
	$page = request('page',false,true,false,1);
	$pagesize = request('pagesize',false,true,false,10);
	$data = compact('title','state');
	$rst = $device_obj->get_device_list($data,$page,$pagesize);
	if(!$rst){
		$rst = array('rows'=>false);
	}
	echo json_encode($rst);
	exit;

}


/************************fun*************************/

/**
* 获取设备列表数据
*
* @param mixed $data
* @param mixed $page
* @param mixed $pagesize
* @return mysql对象
*/
function device_fun_get_device_list($data,$page=1,$pagesize=10){
	global $device_obj;
	return $device_obj->get_device_list($data,$page,$pagesize);
}
/**
* 获取一条设备好数据
* @author xiezhihui 2014-6-10 10:02:42
*
* @param mixed $id
* @return mysql对象
*/
function device_fun_get_device_info($id){
	global $device_obj;
	return $device_obj->get_device_info($id);
}


	/**
	* 设备在作业点的运行登记记录
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	* @return mysql对象
	*/
	function device_fun_get_checkin_list($data,$page=1,$pagesize=10){
		global $device_obj;
		return $device_obj->get_checkin_list($data,$page,$pagesize);
	}

	function device_fun_get_checkin_list_all($data,$cols='*'){
		global $device_obj;
		return $device_obj->get_checkin_list_all($data,$cols);
	}


	function device_fun_get_checkin_search($data,$cols='*'){
		global $device_obj;
		return $device_obj->search_checkin_list($data,$cols);
	}

	function device_fun_get_time_list($data,$page=1,$pagesize=10){
		global $device_obj;
		return $device_obj->get_time_list($data,$page,$pagesize);
	}


	//获取所有计时模式的数据
	function device_fun_get_time_all($data,$page=1,$pagesize=10){
		global $device_obj;
		return $device_obj->get_time_list_all($data,$page,$pagesize);
	}


	//获取路线配置信息 @author leiqianyong 2021-10-19 13:58:50
	function device_fun_get_set_line(){
		global $device_obj;
		return $device_obj->get_set_line();
	}


	/**
	 * 获取登记记录详情
	 * @author leiqianyong 2015-10-12 15:54 PM
	 * @param unknown $id
	 * @return boolean
	 */
	function device_fun_get_record($id){
		global $device_obj;
		return $device_obj->get_record_info($id);
	}



	/**
	* 文件压缩 @author xiezhihui 2014-6-21 10:38:49
	* $aaa=array("../1.txt","../2.txt");
	* Tozip($aaa,"../2.zip",2);
	* @param string $Path 需要压缩的文件或文件夹(文件可为数组)
	* @param mixed $ZipFile 压缩后的zip文件名及存放路径
	* @param mixed $Typ 压缩类型1:文件夹2:文件
	* @param mixed $Todo 后续操作1:压缩后下载;2:存放在服务器上(默认为/@Upload下)
	*/
	Function Tozip($Path,$ZipFile,$Typ=1,$Todo=1){
		global $zip_obj;

		$Path=Str_iReplace("\\","/",($Path));

		IF(Is_Null($Path) Or Empty($Path) Or !IsSet($Path)){Return False;}
		IF(Is_Null($ZipFile) Or Empty($ZipFile) Or !IsSet($ZipFile)){Return False;}

		IF(SubStr($Path,-1,1)=="/"){$Path=SubStr($Path,0,StrLen($Path)-1);}

		OB_end_clean();
		Switch ($Typ){
		Case "1":
			$zip_obj->ZipDir($Path,$ZipFile,$Todo);
			Break;
		Case "2":
			$zip->ZipFile($Path,$ZipFile,$Todo);
			Break;
		}

		IF($Todo==1){
			die();//下载
		}Else{
			Return True;
		}
	}


	function project_fun_bind($pid,$did){
		global $project_obj;
		return $project_obj->project_device_bind_find($pid, $did);
	}

	function project_fun_group_record($pid,$did,$group,$data=false){
		global $project_obj;
		return $project_obj->project_device_group_find($pid, $did,$group,$data);
	}


	function project_fun_total_report($data){
		global $device_obj;
		return $device_obj->get_total_report($data);
	}

	function project_fun_total_time_report($data){
		global $device_obj;
		return $device_obj->get_total_time_report($data);
	}

	function project_fun_total_car_report($data){
		global $device_obj;
		return $device_obj->get_car_time_report($data);
	}
?>
